﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Albatross.WPF.Common.Views {
	public static class Attached {
		#region ViewService
		/// <summary>
		/// Attach the view service to a UserControl View so that the View can provide the ViewService to its ViewModel
		/// The attached property will be inherited by all the children in the logical tree.
		/// </summary>
		public static readonly DependencyProperty ViewServiceProperty = DependencyProperty.RegisterAttached(ViewServicePropertyName, typeof(IViewService), typeof(Attached), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, ViewServicePropertyChanged));
		public const string ViewServicePropertyName = "ViewService";

		public static void SetViewService(UIElement element, IViewService value) {
			element.SetValue(ViewServiceProperty, value);
		}
		public static IViewService GetViewService(UIElement element) {
			return (IViewService)element.GetValue(ViewServiceProperty);
		}
		static void ViewServicePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs args){
			IViewService viewService = args.NewValue as IViewService;
			if (viewService != null && d is UIElement) {
				viewService.LoadElement((UIElement)d);
			}

			if (viewService == null && d is UIElement) {
				viewService = args.OldValue as IViewService;
				if (viewService != null) {
					viewService.UnloadElement((UIElement)d);
				}
			}
		}
		#endregion

		#region CompositeRegion
		public const string CompositeRegionPropertyName = "CompositeRegion";
		/// <summary>
		/// Use to identify a region in the composite view.  Can only be used with a ContentControl
		/// </summary>
		public static readonly DependencyProperty CompositeRegionProperty = DependencyProperty.RegisterAttached(CompositeRegionPropertyName, typeof(string), typeof(Attached), new PropertyMetadata());
		public static void SetCompositeRegion(UIElement element, string value) {
			element.SetValue(CompositeRegionProperty, value);
		}
		public static string GetCompositeRegion(UIElement element) {
			return (string)element.GetValue(CompositeRegionProperty);
		}
		#endregion

		#region IWorkspaceConstruct
		public const string WorkspaceConstructPropertyName = "WorkspaceConstruct";
		public static readonly DependencyProperty WorkspaceConstructProperty = DependencyProperty.RegisterAttached(WorkspaceConstructPropertyName, typeof(IWorkspaceConstruct), typeof(Attached), new PropertyMetadata());
		public static void SetWorkspaceConstruct(UIElement element, IWorkspaceConstruct value) {
			element.SetValue(WorkspaceConstructProperty, value);
		}
		public static IWorkspaceConstruct GetWorkspaceConstruct(UIElement element) {
			return (IWorkspaceConstruct)element.GetValue(WorkspaceConstructProperty);
		}
		#endregion
	}
}
